# -*- coding: UTF-8 -*-
# ! /usr/bin/env python
import json


from rest_framework.authentication import TokenAuthentication

from rest_framework.views import APIView



from backend.common.api_response import JsonResponse


"""
龙盈--解密
调JAVA接口
"""

import requests


class JAVAInterface(APIView):
    authentication_classes = (TokenAuthentication,)
    permission_classes = ()

    def get(self, request):
        # print(find1 in str)
        payload = request.GET.get("content")
        print(payload)

        if payload == '' or payload == None:

            result = "解密失败,请检查json格式"
            return JsonResponse(data={"data": result  # 将返回的结果集返回到前端
                                      }, code="999997", msg=result)
        else:
            PD = 'resContent' in payload#判断是否存在必填参数
            PD1 = '{' in payload  # 判断是否存在必填参数
            PD2 = '}' in payload  # 判断是否存在必填参数

            if PD == False or PD1 == False or PD2 == False:
                result = "解密失败,请检查json格式"
                return JsonResponse(data={"data": result  # 将返回的结果集返回到前端
                                          }, code="999997", msg=result)
            else:
                payload = payload.replace(" ", "")  # 去空白
                try:
                    payload = eval(payload)  # str转dict
                    payload = json.dumps(payload)  # dict转json
                except:
                    result = "解密失败,请检查json格式"
                    return JsonResponse(data={"data": result  # 将返回的结果集返回到前端
                                              }, code="999997", msg=result)

                result = JAVADecrypt(payload)  # 调用解密方法返回response.text
                return JsonResponse(data={"data": result  # 将返回的结果集返回到前端
                                          }, code="999999", msg="成功")


"""获取执行结果详情"""


def JAVACommon(url, payload, headers):

    response = requests.request("POST", url, headers=headers, data=payload)

    print(response.text)
    return response.text


def JAVADecrypt(payload):
    url = "http://192.168.16.12:6789/api/test/rsaDecrypt"

    headers = {
        'Content-Type': 'application/json',
        'Connection': 'keep-alive',
    }

    response = requests.request("POST", url, headers=headers, data=payload)

    print(response.text)
    return response.text
